package com.example.park_management.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.park_management.common.PageParam;
import com.example.park_management.common.Result;
import com.example.park_management.domain.Departments;
import com.example.park_management.domain.Maintenance;
import com.example.park_management.domain.vo.MaintenanceResponseVO;
import com.example.park_management.service.MaintenanceService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xiaoming
 */
@RestController
@RequestMapping("/maintenances")
public class MaintenanceController {

    @Resource
    MaintenanceService maintenanceService;

    @PostMapping("/")
    public Result add(@RequestBody Maintenance maintenance) {
        try{
            if (maintenance.getRoomsId() == null || maintenance.getRoomsId().equals(0)){
                return Result.fail("必填属性为空");
            }

            boolean save = maintenanceService.save(maintenance);
            //123
            return save ? Result.success() : Result.fail();
        }catch (Exception e) {
            return Result.fail("输入格式异常！");
        }
    }

    @PutMapping("/")
    public Result update(@RequestBody Maintenance maintenance) {
        try{
            boolean save = maintenanceService.updateById(maintenance);
            return save ? Result.success() : Result.fail();
        }catch (Exception e) {
            return Result.fail("输入格式异常！");
        }
    }

    @DeleteMapping("/{maintenanceId}")
    public Result delete(@PathVariable Integer maintenanceId) {
        boolean b = maintenanceService.removeById(maintenanceId);
        return b ? Result.success() : Result.fail();
    }

    @PostMapping("/page")
    public Result page(@RequestBody PageParam<MaintenanceResponseVO> pageParam) {
//        long count = maintenanceService.count();
        List<MaintenanceResponseVO> data = maintenanceService.pageQuery(pageParam);
        long count = maintenanceService.countQuery(pageParam);
        return Result.success(data, count);
    }
}
